<template>
  <view class="bg-sd-frd">
    <uni-nav-bar title="注册有礼" fixed status-bar left-icon="left" :border="false" background-color="#F0831E" @clickLeft="clickLeft"></uni-nav-bar>
    <view class="title-top">
      <img src="https://img.qumoyugo.com/webimgbg/picopino-send-frd-bg-new.png" style="width: 100%;height:100%">
    </view>
    <view class="btn-ocp">
      <button type="default" class="natv-button-3" open-type="getPhoneNumber" @getphonenumber="getphonenumber">
        一键加入我们
      </button>
    </view>
  </view>
</template>
<script setup>
import { ref, getCurrentInstance } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { TOKEN } from '@/assets/js/config.js'
import { storage, urlGetObj } from '@/utils/utils'
// import { loginIM } from '@/assets/js/tencentIM/tencentIM.js'
import { useStore } from '@/store'
const store = useStore()
const { $https } = getCurrentInstance().appContext.config.globalProperties
const recommendCustomerId = ref('')
const orginInfo = ref('')
onLoad((option) => {
  if (option.scene) {
    // 通过扫码进入
    $https({
      url: `/common/param/${option.scene}`,
      type: ''
    }).then(res => {
      if (res.code === 0) {
        const _obj = urlGetObj(res.data)
        recommendCustomerId.value = _obj.rmd
        orginInfo.value = _obj.orgin
        // console.log(88888, urlGetObj(res.data))
      }
    })
  } else {
    recommendCustomerId.value = option.rmd
    orginInfo.value = option.orgin
  }
})
const clickLeft = () => {
  uni.navigateBack({
    delta: 1,
    fail: () => {
      uni.switchTab({
        url: '/pages/index'
      })
    }
  })
}
const getphonenumber = (e) => {
  if (e.detail.errMsg !== 'getPhoneNumber:ok') return false
  if (!e.detail.code) { // 版本低下不能获取到信息
    uni.showModal({
      title: '提示',
      content: '微信版本太低请升级版本'
    })
    return false
  }
  wx.login({
    success: (loginRes) => {
      uni.showLoading({
        title: '登录中...'
      })
      $https({
        // 利用code去换取openid
        url: '/js-token',
        type: '',
        data: {
          code: loginRes.code,
          scene: 'mp4'
        }
      }).then(resOpenid => {
        if (resOpenid.code === 0) {
          // 登录关联活动id
          $https({
            url: '/login',
            type: '',
            method: 'POST',
            data: {
              code: e.detail.code,
              type: 'wechat',
              registerPhoneType: 'MP',
              scene: 'mp4',
              openId: resOpenid.data.openid,
              recommendCustomerId: recommendCustomerId.value,
              registerFrom: orginInfo.value
            }
          }).then(res => {
            if (res.code === 0) {
              store.commit('setToken', res.data.token)
              storage.set(TOKEN, res.data.token)
              uni.switchTab({
                url: '/pages/eventsList',
                success: () => {
                  uni.hideLoading()
                }
              })
              $https({
                url: '/profile',
                type: ''
              }).then(profile => {
                if (profile.code === 0) {
                  store.commit('setProfile', profile.data)
                  // const { phone, faceUrl, nickname } = profile.data.attr
                  // loginIM(phone, faceUrl, nickname)
                }
              })
            }
          })
        }
      })
    }
  })
}
</script>
<style scoped lang="scss">
.bg-sd-frd{
  background: #F0831E;
  height: 100vh;
  overflow: hidden;
  .title-top{
    width: 750rpx;
    height: 680rpx;
    margin-top: 50rpx;
  }

}
.btn-ocp{
  width: 407rpx;
  height: 103rpx;
  margin: 140rpx auto 0 auto;
  background: url(https://img.qumoyugo.com/webimgbg/picopino_activeit_aceept_yaoqing_bg_btn-l.png) no-repeat;
  background-size: 100%;
}
.natv-button-3 {
  margin: 0;
  padding: 0;
  background-color: transparent;
  border: none;
  font-size: 34rpx;
  line-height: 102rpx;
  color: #000;
  font-weight: bold;
  &:after {
    border: none;
  }
}
</style>
